# -*- coding: utf8 -*-

import urllib2
from puppy.model import Query, Response

def search(query):
  """Search function for Microsoft Bing.
  
  Parameters:
  
  * query (puppy.model.OpenSearch.Query)
  
  Returns:
  
  * puppy.model.OpenSearch.Response
  
  Raises:
  
  * urllib2.URLError
  
  """
  source_type = 'web'
  url = 'http://api.search.live.net/rss.aspx?&query={0}&source={1}'.format(urllib2.quote(query.search_terms), source_type)
  try:
    response = urllib2.urlopen(url)
    return Response.parse_xml(response.read())
  except urllib2.URLError, e:
    print "Error in Search Service: Bing search failed"
